package com.example.zhinongbao.activity;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.example.zhinongbao.R;
import com.example.zhinongbao.Fragment.OrderManagementFragment;
import com.google.android.material.tabs.TabLayout;

public class OrderManagementActivity extends AppCompatActivity {
    public static final String[] ORDER_STATUSES = {"全部订单", "待付款", "待发货", "待收货", "待评价", "退款/售后"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_management);

        // 初始化Toolbar（非TabLayout，与错误信息一致）
        androidx.appcompat.widget.Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }
        toolbar.setNavigationOnClickListener(v -> onBackPressed());

        // 获取传递的标签索引，默认为0（全部订单）
        int tabIndex = getIntent().getIntExtra("tab_index", 0);

        // 正确初始化TabLayout和ViewPager
        TabLayout tabLayout = findViewById(R.id.tab_layout);
        ViewPager viewPager = findViewById(R.id.view_pager);

        OrderPagerAdapter adapter = new OrderPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);

        // 设置默认选中的标签页
        viewPager.setCurrentItem(tabIndex);
    }

    public static class OrderPagerAdapter extends FragmentPagerAdapter {
        public OrderPagerAdapter(FragmentManager fm) {
            super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        }

        @Override
        public Fragment getItem(int position) {
            return OrderManagementFragment.newInstance(ORDER_STATUSES[position]);
        }

        @Override
        public int getCount() {
            return ORDER_STATUSES.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return ORDER_STATUSES[position];
        }
    }
}